package spring.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class StudentAOP implements MethodBeforeAdvice, MethodInterceptor {
    //这个方法就是我们打算对其进行的增强操作
    public void afterStudy(JoinPoint point) {
        //这个str参数我们该从哪里拿呢？
        System.out.println("学什么" + point.getArgs()[0] + "，Rust天下第一！");
    }

    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我是她的家长，他不能学Java，必须学Rust，这是为他好");
        Object value = joinPoint.proceed(new Object[]{"Rust"});
        if (value.equals("Java")) {
            System.out.println("听话，学Rust以后进大厂！");
            value = "Rust";
        }
        return value;
    }

    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("通过Advice实现AOP");
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object value = invocation.proceed();   //跟之前一样，需要手动proceed()才能调用原方法
        return value+"增强";
    }
}
